﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Dataset;
using DataCentral.Dataset.DataCentralDatasetTableAdapters;

namespace DataCentral.UserControls
{
    public partial class EditSaleOrdersControl : DataCentral.CommonCenterControl
    {
        public EditSaleOrdersControl()
        {
            InitializeComponent();
        }

        private decimal FindRoundIDBySalePerson(decimal salePersonID)
        {
            DataCentralDataset.SalespersonRow drSaleperson = dsCentral.Salesperson.FindBySalePersonID(salePersonID);
            if (drSaleperson != null)
                return drSaleperson.RoundID;
            else return 0;
        }

        private void StandardizationData()
        {
            foreach (DataCentralDataset.SlsOrdDspRow drSaleOrder in dsCentral.SlsOrdDsp)
            {
                //Update CuryLineDiscAmt
                if (drSaleOrder.IsCuryLineDiscAmtNull())
                    drSaleOrder.CuryLineDiscAmt = 0;
                //Update RoundID
                if (drSaleOrder.IsRoundIDNull() || drSaleOrder.RoundID == 0 || string.IsNullOrEmpty(drSaleOrder.RoundID.ToString()))
                {
                    drSaleOrder.RoundID = FindRoundIDBySalePerson(drSaleOrder.SlsPerId);
                }
            }
        }

        private void EditSaleOrders_Load(object sender, EventArgs e)
        {
            dtpFromDate.DateTime = DateTime.Now;
            dtpToDate.DateTime = DateTime.Now;
            daSaleOrders.Fill(dsCentral.SlsOrdDsp, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
            SalespersonTableAdapter daSalePerson = new SalespersonTableAdapter();
            daSalePerson.Fill(dsCentral.Salesperson);
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            daSaleOrders.Fill(dsCentral.SlsOrdDsp, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
        }

        private void btnStandardization_Click(object sender, EventArgs e)
        {
            StandardizationData();
            daSaleOrders.Update(dsCentral.SlsOrdDsp);
        }
    }
}
